<template>
    <div class="card-page">
        <tool-bar nav="" :title='action_name + "Git服务器"'>
            <el-button size="small" @click="$router.push('/dev/git-server/add')" v-if="$props.action=='edit'">新建</el-button>
            <el-button size="small" @click="save_click" type="primary"> 保存</el-button>
        </tool-bar>
        <el-row :gutter="12" style="width: 100%;" justify="space-around">
            <el-col :span="12">
                <el-card shadow="always">

                    <template #header><span>基本信息</span></template>
                    <kv label="name">
                        <el-input v-model="info.name" chk="*"/>
                    </kv>
                    <kv label="地址">
                        <el-input v-model="info.host"/>
                    </kv>
                    <kv label="类别">
                        <selector v-model="info.gitServerType" :data="['Gitea','Gitlab']" chk="*"/>
                    </kv>

                    <kv label="code">
                        <el-input v-model="info.code"/>
                    </kv>
                </el-card>
            </el-col>
            <el-col :span="12">
                <el-card shadow="always">
                    <template #header> <span>扩展信息</span> </template>
                    <kv>
                        <template #k>
                            <div class="k" v-if="info.gitServerType == 'Gitlab'">Gitlab access_token
                                <remark>
                                    后续会弃用该处的设置 <br/>
                                    每个git仓库，要单独设置 Access token，这是默认的 Access token。<br/>
                                    Gitlab -> 头像 -> Preferences -> Access tokens
                                </remark>
                            </div>

                            <div class="k" v-else>Gitea access_token
                                <remark>
                                    后续会弃用该处的设置 <br/>
                                    每个git仓库，要单独设置 Access token，这是默认的 Access token。 <br/>
                                    Gitea -> 设置 -> 应用 -> 管理access tokens
                                </remark>
                            </div>
                        </template>

                        <el-input v-model="info.accessToken" type="textarea"/>
                    </kv>

                    <kv>
                        <template #k>
                        <div   class="k">jenkins的git credential
                            <remark>
                                后续会弃用该处的设置 <br/>
                                每个git仓库，要单独设置 Access token，默认使用 git仓库的 access token 获取<br/>
                                jenkins -> 系统管理 -> 安全 -> Manage Credentials -> 全局凭据Id
                            </remark>
                        </div></template>
                        <el-input v-model="info.jenkinsCredential" disabled/>
                    </kv>

                    <kv label="备注">
                        <el-input v-model="info.remark" type="textarea"/>
                    </kv>
                </el-card>
            </el-col>
        </el-row>
    </div>
</template>

<script setup>
/**
 * Created by CodeGenerator  at 2021-04-12 12:10:50
 */



//定义 vue2的this,当前组件
let $self = getCurrentInstance();

//定义 $attrs
let $attrs = useAttrs()


//定义事件
let $emit = defineEmits(["update:modelValue", "loaded"]);

//定义属性
var $props = defineProps({
    id: {type: String, default: ""},
    action: {type: String, default: ""}
});

//定义data
let $data = reactive({
    info: {}, //子对象需要声明。
})


const info = $computed(() => $data.info);

const loadData = function () {
    if (!$props.id) return;
    jv.$http.post("/dev/git-server/detail/" + $props.id).then(res => {
        $data.info = res.data.data;
    });
}

const save_click = function () {
    //校验
    if (jv.chk_main($data) == false) {
        return;
    }

    jv.$http.post("/dev/git-server/save", $data.info).then(res => {
        //[axios拦截器中已处理了异常]。
        jv.info(action_name + " 成功");
        if (this.action == "add") {

            jv.$router.push("/dev/git-server/edit/" + res.data.data)
        } else if (this.action == "edit") {
            jv.$router.push("/dev/git-server/list")
        }
    })
}


const action_name = $computed(() => {
    return {add: "添加", edit: "修改"}[$props.action]
});

loadData();

</script>
